package com.yw.smartcityforjava.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.api.allservices.ServicesResponse;

import java.util.ArrayList;
import java.util.List;

public class ServiceGridAdapter extends BaseAdapter {

    private List<ServicesResponse.RowsBean> datas = new ArrayList<>();

    public void setDatas(List<ServicesResponse.RowsBean> datas) {
        this.datas = datas;
    }

    public List<ServicesResponse.RowsBean> getDatas() {
        return datas;
    }

    @Override
    public int getCount() {
        if(datas == null || datas.size() == 0)
            return 0;
        else
            return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ServicesResponse.RowsBean data = datas.get(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iconIv = view.findViewById(R.id.icon);
            viewHolder.nameTv = view.findViewById(R.id.name);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if(position == datas.size()-1){
            viewHolder.iconIv.setImageResource(R.drawable.menu_services);
        }else{
            Glide.with(parent.getContext()).load(App.baseUrl + data.getImgUrl()).into(viewHolder.iconIv);
        }

        viewHolder.nameTv.setText(data.getServiceName());
        return view;
    }

    private class ViewHolder{
        public ImageView iconIv;
        public TextView nameTv;
    }
}
